package com.mz361.modules.system.service.impl;

import com.mz361.common.data.PageSort;
import com.mz361.common.enums.StatusEnum;
import com.mz361.modules.system.domain.PayOrder;
import com.mz361.modules.system.repository.PayOrderRepository;
import com.mz361.modules.system.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liaomch
 * @date 2024/05/07
 */
@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Autowired
    private PayOrderRepository payOrderRepository;

    /**
     * 根据ID查询数据
     *
     * @param id 主键ID
     */
    @Override
    public PayOrder getById(Long id) {
        return payOrderRepository.findById(id).orElse(null);
    }

    @Override
    public PayOrder getByOrderId(String orderId) {
        return payOrderRepository.findByOrderId(orderId);
    }

    @Override
    public PayOrder getByStandardNoAndMemeberId(String standardNo, Long memeberId) {
        return payOrderRepository.findByMemeberIdAndPaidPackageTag(memeberId, standardNo);
    }

    @Override
    public PayOrder getByStandardNoAndMemeberIdNotSuccess(String standardNo, Long memeberId) {
        return payOrderRepository.findByMemeberIdAndPaidPackageTagNotSuccess(memeberId, standardNo);
    }

    @Override
    public PayOrder findByCompanyAndPaidPackageTag(String clz, Long companyId) {
        return payOrderRepository.findByCompanyAndPaidPackageTag(companyId, clz);
    }

    @Override
    public PayOrder findByCompanyAndPaidPackageTagNotSuccess(String clz, Long companyId) {
        return payOrderRepository.findByCompanyAndPaidPackageTagNotSuccess(companyId, clz);
    }

    @Override
    public List<PayOrder> getByUserId(Long userId) {
        return payOrderRepository.findAllByMemeberIdAndResultStatusIsAndInvoiceStatus(userId, "SUCCESS", "0");
    }

    @Override
    public List<PayOrder> getByMemeberId(Long memeberId) {
        return payOrderRepository.findAllByMemeberId(memeberId);
    }

    @Override
    public Long getTotalPerformancebySourcekey(String sourceKey) {
        return payOrderRepository.getTotalPerformancebySourcekey(sourceKey);
    }

    @Override
    public Long getTotalPerformance() {
        return payOrderRepository.getTotalPerformance();
    }

    @Override
    public Long getCountOrderbySourcekey(String sourceKey) {
        return payOrderRepository.getCountOrderbySourcekey(sourceKey);
    }

    @Override
    public Long getCountOrder() {
        return payOrderRepository.getCountOrder();
    }

    @Override
    public Long getTotalPerformancebyInviteCode(String inviteCode) {
        return payOrderRepository.getTotalPerformancebyInviteCode(inviteCode);
    }

    @Override
    public Long getCountOrderbyInviteCode(String inviteCode) {
        return payOrderRepository.getCountOrderbyInviteCode(inviteCode);
    }


    /**
     * 获取分页列表数据
     *
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<PayOrder> getPageList(Example<PayOrder> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return payOrderRepository.findAll(example, page);
    }

    /**
     * 保存数据
     *
     * @param payOrder 实体对象
     */
    @Override
    public PayOrder save(PayOrder payOrder) {
        return payOrderRepository.save(payOrder);
    }

    @Override
    public void saveAll(List<PayOrder> payOrders) {
        payOrderRepository.saveAll(payOrders);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return payOrderRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    public Long countAll() {
        return null;
    }


}